//手机号正则表达式
var phoneReg = /^1[345789][0-9]{9}$/;
//zz验证码正则表达式
var codeReg =/^[0-9]{6}$/;
var pwd0Reg = /^.{6,16}$/;
var pwd1Reg =/[0-9]+/;
var pwd2Reg =/[a-zA-Z]+/;
var pwd3Reg =/~!@#\$%\^&*\(\)_+={}[]/;
var uaerNameReg =/^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
//表示是否同意注册协议
var isPhoneRight = false;
var isYZMRight = false;
var isPwd1Right = false;
var isPwd2Right = false;
var isName2Right = false;

var timer;
var t = 60;

//功能1：向用户提示手机号码的格式是否正确性
phone.onblur = function(){
    var phoneValue=phone.value;

    var phoneerror = document.getElementById("phoneerror");
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML = "手机号码有误!";
        isPhoneRight = false;
    }else{
        $.get(
            "http://test.hfjava.com/shop/userphone?phone="+phoneValue,
            {},
            function (data) {
                if(data==true){
                    phoneerror.innerHTML = "<img src='images/dui.png' style='width:14px'/>";
                    isPhoneRight = true;
                }else{
                    phoneerror.innerHTML = "手机号码已注册!";
                    isPhoneRight = false;
                }
            }
        );

    }
    checkAll();
};

//功能2：根据手机号码格式正确性，决定是否启用验证码按钮
phone.onkeyup = function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)) {
        yanzhengmaBtn.setAttribute("disabled", "disabled");
        yanzhengmaBtn.style.color = "";
        isPhoneRight = false;
    }else{
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color="#1a65e6";
        isPhoneRight = true;
    }
    checkAll();
};

//功能3：发送验证码功能
//点击后倒计时60秒，在此期间该按钮应该禁用
yanzhengmaBtn.onclick=function(){
    yanzhengmaBtn.setAttribute("disabled", "disabled");
    yanzhengmaBtn.style.color="";
    yanzhengmaBtn.value="重新发送60";

    timer = setInterval(daojishi,1000)
};
function daojishi(){
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color="#1a65e6";
        yanzhengmaBtn.value="获取验证码";
        t = 60;
        return;
    }
    yanzhengmaBtn.value = "重新获取验证码"+t;
}


//功能4：验证验证码正确性
yzm.onkeyup = function(){
    var yzmValue = yzm.value;
    if(yzmValue.length==0){
        yzmerror.innerHTML= "请填写验证码!";
        isYZMRight=false;
        checkAll();
        return;
    }
    if(!codeReg.test(yzmValue)){
        yzmerror.innerHTML="验证码格式错误！";
        isYZMRight = false;
        checkAll();
        return;
    }
    $.ajax({
        url:"http://test.hfjava.com/shop/yzm",
        dataType:"jsonp",
        success:function(data){
            if(data.code!=yzmValue){
                yzmerror.innerHTML="验证码错误！";
                isYZMRight = false;
            }else{
                yzmerror.innerHTML = "<img src='images/dui.png' style='width:14px'/>";
                isYZMRight = true;
            }
            checkAll();
        }
    });
};

//功能5：密码1验证格式有效性
var pwd1 = document.getElementById("pwd1");
pwd1.onkeyup = function(){
    var pwd1Value = pwd1.value;
    var rightCount = 0;

    var lengthValid = pwd0Reg.test(pwd1Value);
    var numberValid = pwd1Reg.test(pwd1Value);
    var letterValid = pwd2Reg.test(pwd1Value);
    var symbolValid = pwd3Reg.test(pwd1Value);

    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2) {
        pwd1error.innerHTML = "密码格式错误！";
        isPwd1Right = false;
    }else{
        pwd1error.innerHTML="<img src='images/dui.png' style='width:14px'/>";
        isPwd1Right = true;
    }
    checkPwd2();
    checkAll();
};

//功能6：密码2验证与密码1是否一致
pwd2.onkeyup = function() {
    checkPwd2();
};
function checkPwd2(){
    var pwd1Value = pwd1.value;
    var pwd2Value = pwd2.value;
    if(pwd1Value!=pwd2Value){
        pwd2error.innerHTML="两次密码不一致";
        isPwd2Right = false;
    }else{
        pwd2error.innerHTML = "<img src='images/dui.png' style='width:14px'/>";
        isPwd2Right = true;
    }
    checkAll();
}
//昵称检测
username.onkeyup = function () {
    var userNameValue = username.value;
    if(userNameValue.length==0){
        usernameerror.innerHTML="昵称不能为空！";
        isName2Right = false;
    }else{
        usernameerror.innerHTML = "<img src='images/dui.png' style='width:14px'/>";
        isName2Right = true;
    }
};

//功能7：获得用户是否同意注册协议
//功能8：检测是否可以注册
function checkAll(){
    var isRight = isName2Right&&isPhoneRight&&isYZMRight&&isPwd1Right&&isPwd2Right;
    if(isRight) {
        submitBtn.removeAttribute("disabled");
        submitBtn.style.background="#1a65e6";
        submitBtn.style.color="#fff";
    }else{
        submitBtn.setAttribute("disabled","disabled");
    }
}

//功能9：实现注册功能
submitBtn.onclick=function(){
    $.post("http://test.hfjava.com/shop/adduser",$("form").serialize(),function(data){
        if(data=="Saved"){
            console.log("注册成功！");
            alert("注册成功！立即跳到登录页面...");
            location="login.html";
        }
    });
};
